<template>
    <div class="seller" ref="seller">
      <div class="seller-wrapper">
        <!--商家内容介绍 -->
        <div class="overview">
          <!--直接使用 h1 or h2 -->
          <h1 class="title">{{seller.name}}</h1>
          <div class="desc">
            <!--评分组件 -->
            <star :size="36" :score="seller.score"></star>
            <span class="text">({{seller.ratingCount}})</span>
            <span class="text">月售{{seller.sellCount}}单</span>
          </div>
          <!--为啥不采用flex布局呢? -->
            <ul class="remark">
              <li class="block">
                <h2>起送价</h2>
                <div class="content">
                  <span class="stress">{{seller.minPrice}}</span>元
                </div>
              </li>
              <li class="block">
                <h2>商家配送</h2>
                <div class="content">
                  <span class="stress">{{seller.deliveryPrice}}</span>元
                </div>
              </li>
              <li class="block">
                <h2>平均配送时间</h2>
                <div class="content">
                  <span class="stress">{{seller.deliveryPrice}}</span>分钟
                </div>
              </li>
            </ul>
          <!--收藏 当页面刷新的时候，刚刚收藏又变成未收藏，需要将内容存到localstorage中-->
          <div class="favorite" @click="toggleFavorite">
            <!--动态绑定class并且激活active状态 -->
            <span class="icon-favorite" :class="{'active':favorite}"></span>
            <!--通过计算属性将text内容与图标进行绑定 -->
            <span class="text">{{favoriteText}}</span>
          </div>
        </div>
        <!--区域块 -->
        <split></split>
        <!--公共与活动 -->
        <div class="bulletin">
          <h1 class="title">公告与活动</h1>
          <div class="content-wrapper border-1px">
            <div class="content">{{seller.bulletin}}</div>
          </div>
          <!--促销活动列表 -->
          <ul v-show="seller.supports" class="supports ">
          <li v-for="(item,index) in seller.supports" :key="index" class="support-item border-1px">
            <!-- 动态绑定class样式 -->
            <span class="icon" :class="classMap[seller.supports[index].type]"></span>
            <span class="text">{{item.description}}</span>
          </li>
          </ul> 
        </div>
        <!--区域块 -->
        <split></split>
        <!--商家实景 -->
        <div class="pics">
          <div class="title">商家实景</div>
          <!--ref需要设置成picWrapper 而不是pic-wrapper -->
          <div class="pic-wrapper" ref="picWrapper">
            <ul class="pic-list"  ref="picList">
              <li class="pic-item" v-for="(item,index) in seller.pics" :key="index">
                <img :src="item" width="120" height="90" />
              </li>
            </ul>
          </div>
        </div>
        <!--区域块 -->
        <split></split>
        <!--商家信息 -->
        <div class="info">
          <h1 class="title border-1px">商家信息</h1>
          <ul>
            <li class="info-item border-1px" v-for="(item,index) in seller.infos" :key="index">
              <span class="text">{{item}}</span>
            </li>
          </ul>
        </div>
      </div>
    </div>
</template>

<script>
import BScroll from 'better-scroll';
import star from '../../components/base/star/star.vue';
import split from '../base/split/split.vue';
import {saveToLocall,loadFromLocal} from '../../utils/storage.js';

export default {
  props: {
    seller: {
      type: Object
    }
  },
  components: {
    star,
    split
  },
  data() {
    return {
      favorite: (() =>{
        return loadFromLocal(this.seller.id,'favorite',false)
      })()
    };
  },
  created(){
    console.log('使用钩子函数created');
    this.classMap = ['decrease', 'discount', 'special', 'invoice', 'guarantee'];
    //this.loadScroll()
  },
  mounted() {
    //vue2.0中 使用mounted代替ready,但不能保证钩子函数中滴this.$el在document中，为此引入vue.$nextTick
    this._loadScroll();
    this._initPics(); // vue.esm.js?efeb:591 [Vue warn]: Error in mounted hook: "TypeError: Cannot read property 'style' of undefined" 原因是ref='pic-list'模式
  },
  computed: {
    //第一种(es5)写法
    favoriteText() {
      return this.favorite ? '已收藏' : '收藏'
    }

    // 第二种(es6)写法 在vue2.x中的computed中不要使用箭头函数，因为箭头函数中的this指的是上一层不是本层
    //                vue的计算属性computed默认只有getter,需要使用getter的时候需要自己加一个setter
    //                使用状态管理vuex进行使用mapState() Or mapGetters() 
    // ...mapState({
    //     user_name: state => state.user_name,
    //     user_id: state => state.user_id,
    //     user_source: state => state.user_source,
    // })

  },
  methods: {
    _loadScroll() {
       //渲染dom
      this.$nextTick(() => {// 代码保证 this.$el 在 document 中
      console.log('使用钩子函数mounted');
        if(!this.scroll){
          this.scroll = new BScroll(this.$refs.seller,{
            click:true
          });
        }else{
          this.scroll.refresh();
        }
      });
    },
    //商家实景,横向滚动
    _initPics(){
      console.log(this.seller.pics.length);
      if(this.seller.pics){
        let picWidth = 120;
        let margin = 6;
        let width = (picWidth + margin) * this.seller.pics.length - margin//如果实现左右滚动，需要设置ul的宽度大于pic-wrapper的宽度
        //设置ul的宽度
        this.$refs.picList.style.width = width + 'px';
        if(!this.picScroll){
          this.picScroll = new BScroll(this.$refs.picWrapper,{
            scrollX:true, // 横向滚动
            eventPassthrough: 'vertical'    // 在这个区块忽略竖向滚动
          });
        }else{
          this.picScroll.refresh();
        }
      }
    },
    //收藏
    toggleFavorite() {
       //兼容pc
      if(!event._constructed){
        return;
      }
      this.favorite = !this.favorite;
      //保存收藏
      console.log('this.seller.id的值  ' + this.seller.id);
      saveToLocall(this.seller.id, 'favorite', this.favorite);
      //setStorage(this.seller.id,this.favorite);
    }
  }
};
</script>

<style lang="scss" rel="stylesheet/scss">
@import "../../assets/style/mixin.scss";

.seller {
    position: absolute;
    top: 174px;
    left: 0;
    bottom: 0;
    width: 100%;
    overflow: hidden;//超出部分隐藏
    .overview {
      position: relative; // ?
      padding: 18px;
      font-size: 0;
      .title {
        margin-bottom: 8px; // ?
        line-height: 14px;
        font-size: 14px;
        color: rgb(7, 17, 27);
      }
      .desc {
        padding-bottom: 18px ;
        // line-height: 18px; 放到此处容易导致高度过高，当高度过高的时候，将高度存放到子元素中
        @include border-1px(rgba(7, 17, 27, 0.1));
        font-size: 0;
        color: rgb(7, 17, 27);
        .star {
          display: inline-block;
          vertical-align: top;
          margin-right: 8px;
        }
        .text {
          display: inline-block;
          vertical-align: top;
          margin-right: 12px;
          line-height: 18px;
          font-size: 10px;
          color: rgb(77, 85, 93);
        }
      }
      .remark {
        display: flex; //自适应布局
        padding-top: 18px;
        .block {
          flex: 1;
          text-align: center;
          border-right: 1px solid rgba(7, 17, 27, 0.1);
          &:last-child {
            border: 0;// 最后一个模块没有右边框
          }
          h2 {
            margin-bottom: 4px;
            font-size: 10px;
            line-height: 10px;
            color: rgb(147, 153, 159);
          }
          .content {
            line-height: 24px;
            font-size: 10px;
            color: rgb(7, 17, 27);
            .stress {
              font-size: 24px;
            }
          }
        }
      }
      .favorite {
        position: absolute;//固定
        top:18px;
        right: 10px;
        bottom: 0;
        width: 50px; // 不然图片与文字随着变动
        text-align: center; // 父类定义center即将文字居中显示
        font-size: 0;//span与span直接需要去掉空的占位符
        .icon-favorite {
          display: block;
          margin-bottom: 4px;
          line-height: 24px;
          font-size: 24px;
          color: #d4d6d9;
          &.active {
            color: rgb(240, 20, 20);
          }
        }
        .text {
          line-height: 10px;
          font-size: 10px;
          color: rgb(77, 85, 93);
        }
      }
    }
    .bulletin {
      padding: 18px 18px 0 10px;
      font-size: 0;
      .title {
        margin-bottom: 8px; // ?
        line-height: 14px;
        font-size: 14px;
        color: rgb(7, 17, 27);
      }
      .content-wrapper {
        padding: 0 12px 16px 12px; // ?
        @include border-1px(rgba(7, 17, 27, 0.1));// 由于边距嵌套内容,设置content边框
        .content {
          line-height: 24px;
          font-size: 12px;
          color: rgb(240, 20, 20);
        }
      }
    .supports {
        .support-item {
          padding: 16px 12px;
          font-size: 0;
          @include border-1px(rgba(7, 17, 27, 0.1));
          &:last-child {
            @include border-none();
          }
        }
        .icon {
          display: inline-block;
          width: 16px;
          height: 16px;
          vertical-align: top;
          margin-right: 6px;
          background-size: 16px 16px;
          background-repeat: no-repeat;
          &.decrease {
            @include bg-img('decrease_4');
          }
          &.discount {
            @include bg-img('discount_4');
          }
          &.guarantee {
            @include bg-img('guarantee_4');
          }
          &.invoice {
            @include bg-img('invoice_4');
          }
          &.special {
            @include bg-img('special_4');
          }
        }
        .text {
          line-height: 16px;
          font-size: 12px;
          color: rgb(7, 17, 27);
        }
      }
    }
    .pics {
      padding: 18px 0 18px 18px;
      .title {
        margin-bottom: 12px; // ?
        line-height: 14px;
        font-size: 14px;
        color: rgb(7, 17, 27);
      }
      .pic-wrapper {
        width: 100%;
        overflow: hidden;
        white-space: nowrap;//当图片列表超过屏幕时，不会换行
        .pic-list {
          font-size: 0;//去除图片直接的空位符
          .pic-item {
            display: inline-block; //由于li的特性，因此需要设置display,否则导致上下排列
            margin-right: 6px;
            width: 120;
            height: 90;
            &:last-child {
              margin: 0;
            }
          }
        }
      }
    }
    .info {
      padding: 18px 18px 0 18px; // padding的位置搞错容易导致距离出界限
      .title {
        // margin-bottom: 12px; // ? 将margin改成padding
        padding-bottom: 12px;
        @include border-1px(rgba(7, 17, 27, 0.1));
        line-height: 14px;
        font-size: 14px;
        color: rgb(7, 17, 27);
      }
      .info-item {
        padding: 16px 12px;
        font-size: 0;
        @include border-1px(rgba(7, 17, 27, 0.1));
        &:last-child {
          @include border-none();
        }
        .text {
          display: inline-block;
          line-height: 16px;
          font-size: 12px;
          color: rgb(7, 17, 27);
        }
      }
    }
}
</style>
